

$(function(){
	function total_fraction(){
		var total_fraction = 0;
		$.each($(".template_box").find(".number"), function(i, obj){
			var number = $(obj).val();
			var fraction = $(".template_box").find(".fraction").eq(i).val();
			if(number > 0 && fraction > 0){
				total_fraction += number * fraction;
			}
		})
		$(".total i").html(total_fraction)
	}

	total_fraction();

	$(".next").click(function(){
		$(this).closest(".add_step").hide().next().show();
		$("#name_text").html($("#name").val());
	})

	//模版总分计算
	$(".template_box input[type=text]").on("change",function(){
		total_fraction();
	})

	//获取模版信息
	layui.use(['form', 'jquery'], function(){
    	var form = layui.form(),$ = layui.jquery;

    	$("#select_subject").click(function(){
			if($.trim($("#name").val()) == ''){
				layer.alert('名称不能为空', {
			  		skin: 'layui-layer-molv' //样式类名
				  	,closeBtn: 0
				  	,anim: 4 //动画类型
				});
				return false;
			}

			

			if($("#end_time").length > 0 && $.trim($("#end_time").val()) == ''){
				layer.alert('结束时间不能为空', {
			  		skin: 'layui-layer-molv' //样式类名
				  	,closeBtn: 0
				  	,anim: 4 //动画类型
				});
				return false;
			}
			
			var href=$("#name").closest(".narrow").attr("add_url");
			if (typeof(href) == "undefined"){
				href = "/tiku/add_test_volume/"+$("#subject_id").val()+"/"+$("#name").val();
			}else{
				var end_time = $("#end_time").val();
				var time = Date.parse(new Date(end_time)) / 1000;
				var prompt = $("#prompt").val();
				var classes = '';
				$('input:checkbox[name="classes[]"]:checked').each(function(i){
		       		if(0==i){
		        		classes = $(this).val();
			       	}else{
			        	classes += ("_"+$(this).val());
			       	}
		      	});
		      	if(classes == ''){
		      		alert("至少选择一个班级");
		      		return false;
		      	}
		      	
				href += "/"+$("#subject_id").val()+"/"+$("#name").val()+"/"+time+"/"+encodeURIComponent(prompt)+"/"+classes;
			}
			window.location.href=href;
		})


    	form.on('select(template_id)', function(data){
    		var template_id = $(data.elem).val();
			if(template_id > 0){
				$("#template_name").hide();
				
				$.ajax({
					type: "POST",
					url: "/tiku/get_template_content",
					dataType:"json",
					data:{'template_id':template_id},
					async:false,
					success: function(result){
						// layer.close(ii);
						if(result.error == 0){
							$(".template_box input[type=text]").attr("disabled", true)
							$.each(result.content,function(i,n){
								$(".template_box input[name='number["+i+"]']").val(n.number)
								$(".template_box input[name='fraction["+i+"]']").val(n.fraction)

							});//topic_list_box
							load_topic();
						}else{
							layer.alert(result.msg, {
						  		skin: 'layui-layer-molv' //样式类名
							  	,closeBtn: 0
							  	,anim: 4 //动画类型
							});
						}
					}
				});
			}else{
				if(template_id == ''){
					$("#template_name").hide();
				}else{
					$("#template_name").show();
				}
				$(".template_box input[type=text]").attr("disabled", false);

				$(".template_box input[name='number[1]']").val(20);
				$(".template_box input[name='fraction[1]']").val(1);

				$(".template_box input[name='number[2]']").val(20);
				$(".template_box input[name='fraction[2]']").val(2);

				$(".template_box input[name='number[3]']").val(20);
				$(".template_box input[name='fraction[3]']").val(1);

				$(".template_box input[name='number[4]']").val(20);
				$(".template_box input[name='fraction[4]']").val(1);

				$(".template_box input[name='number[7]']").val(20);
				$(".template_box input[name='fraction[7]']").val(1);

				$(".template_box input[name='number[8]']").val(20);
				$(".template_box input[name='fraction[8]']").val(1);

				$(".template_box input[name='number[9]']").val(20);
				$(".template_box input[name='fraction[9]']").val(1);

				load_topic();
			}
		});

		form.on('radio(create_topic_type)', function(data){
			load_topic();
		})

		form.on('select(source_type)', function(data){
			load_topic();
		})

		//保存模版
		$(".add_template").click(function(){
			if($("#template_id").val() > 0){
				return false;
			}

			if($("#template_name_text").val() == ''){
				layer.alert("缺少模版名称", {
			  		skin: 'layui-layer-molv' //样式类名
				  	,closeBtn: 0
				  	,anim: 4 //动画类型
				});
				return false;
			}

			if(total_fraction > 200){
				layer.alert("总分不能超过200分", {
			  		skin: 'layui-layer-molv' //样式类名
				  	,closeBtn: 0
				  	,anim: 4 //动画类型
				});
				return false;
			}

			var number_one = $(".template_box input[name='number[1]']").val();
			var fraction_one = $(".template_box input[name='fraction[1]']").val();

			var number_two = $(".template_box input[name='number[2]']").val();
			var fraction_two = $(".template_box input[name='fraction[2]']").val();

			var number_three = $(".template_box input[name='number[3]']").val();
			var fraction_three = $(".template_box input[name='fraction[3]']").val();

			var number_four = $(".template_box input[name='number[4]']").val();
			var fraction_four = $(".template_box input[name='fraction[4]']").val();

			var number_seven = $(".template_box input[name='number[7]']").val();
			var fraction_seven = $(".template_box input[name='fraction[7]']").val();

			var number_eight = $(".template_box input[name='number[8]']").val();
			var fraction_eight = $(".template_box input[name='fraction[8]']").val();

			var number_nine = $(".template_box input[name='number[9]']").val();
			var fraction_nine = $(".template_box input[name='fraction[9]']").val();

			$.ajax({
				type: "POST",
				url: "/tiku/save_test_volume_template",
				dataType:"json",
				data:{'number[1]':number_one,'number[2]':number_two,'number[3]':number_three,'number[4]':number_four,'number[7]':number_seven,'number[8]':number_eight,'number[9]':number_nine,'fraction[1]':fraction_one,'fraction[2]':fraction_two,'fraction[3]':fraction_three,'fraction[4]':fraction_four,'fraction[7]':fraction_seven,'fraction[8]':fraction_eight,'fraction[9]':fraction_nine,'template_name':$("#template_name_text").val()},
				success: function(result){
					// layer.close(ii);
					layer.alert(result.msg, {
				  		skin: 'layui-layer-molv' //样式类名
					  	,closeBtn: 0
					  	,anim: 4 //动画类型
					});
					if(result.error == 0){
						var html = "<option value='"+result.template_id+"' selected='selected'>"+result.template_name+"</option>";
						$("#template_id").append(html);
						load_topic();
					}
				}
			});
		})

		function load_topic(){
			if($("input[name='create_topic_type']:checked").val() == 2 && $("#template_id").val() > 0){
				var ii = layer.load();
				$("#topic_list").show();
				
				if($(".chapter_id").length > 0){
					var chapter_id = '';
					$(".chapter_id").each(function(i){
				   		chapter_id += $(this).val();
				   		if(i < $(".chapter_id").length - 1){
				   			chapter_id += '.';
				   		}
					});
				}else{
					var chapter_id = 0;
				}
				var src = "/tiku/get_list/"+$("#source_type").val()+"/"+chapter_id;
				var type_element = $("#topic_list_box").contents().find("#type");
				var subject_text_element = $("#topic_list_box").contents().find("#subject_text");
				if(type_element.length > 0){
					src += "/" + type_element.val();
				}
				if(subject_text_element.length > 0){
					src += "/" + subject_text_element.val();
				}
				layer.close(ii);
				$("#topic_list_box").attr('src', src);
				$("#topic_list_box").show();
				$("#type_box_1").html('');
				$("#type_box_2").html('');
				$("#type_box_3").html('');
				$("#type_box_4").html('');
				$("#type_box_5").html('');
				$("#type_box_6").html('');
				$("#type_box_7").html('');
				$("#type_box_8").html('');
				$("#type_box_9").html('');
				var type_box_1 = parseInt($("input[name='number[1]']").val()) + 1;
				var type_box_2 = parseInt($("input[name='number[2]']").val()) + 1;
				var type_box_3 = parseInt($("input[name='number[3]']").val()) + 1;
				var type_box_4 = parseInt($("input[name='number[4]']").val()) + 1;
				var type_box_5 = parseInt($("input[name='number[5]']").val()) + 1;
				var type_box_6 = parseInt($("input[name='number[6]']").val()) + 1;
				var type_box_7 = parseInt($("input[name='number[7]']").val()) + 1;
				var type_box_8 = parseInt($("input[name='number[8]']").val()) + 1;
				var type_box_9 = parseInt($("input[name='number[9]']").val()) + 1;
				var html1 = '';
				for (var i = 1; i < type_box_1; i++) {
					if(i < 10){
						i = "0"+i;
					}
					html1 += '<input class="type_1 type_span" title="'+i+'" disabled type="checkbox" />';
				}
				$("#type_box_1").html(html1);

				var html2 = '';
				for (var i = 1; i < type_box_2; i++) {
					if(i < 10){
						i = "0"+i;
					}
					html2 += '<input class="type_2 type_span" title="'+i+'" disabled type="checkbox" />';
				}
				$("#type_box_2").html(html2);

				var html3 = '';
				for (var i = 1; i < type_box_3; i++) {
					if(i < 10){
						i = "0"+i;
					}
					html3 += '<input class="type_3 type_span" title="'+i+'" disabled type="checkbox" />';
				}
				$("#type_box_3").html(html3);

				var html4 = '';
				for (var i = 1; i < type_box_4; i++) {
					if(i < 10){
						i = "0"+i;
					}
					html4 += '<input class="type_4 type_span" title="'+i+'" disabled type="checkbox" />';

				}
				$("#type_box_4").html(html4);

				var html5 = '';
				for (var i = 1; i < type_box_5; i++) {
					if(i < 10){
						i = "0"+i;
					}
					html5 += '<input class="type_5 type_span" title="'+i+'" disabled type="checkbox" />';

				}
				$("#type_box_5").html(html5);

				var html6 = '';
				for (var i = 1; i < type_box_6; i++) {
					if(i < 10){
						i = "0"+i;
					}
					html6 += '<input class="type_6 type_span" title="'+i+'" disabled type="checkbox" />';

				}
				$("#type_box_6").html(html6);

				var html7 = '';
				for (var i = 1; i < type_box_7; i++) {
					if(i < 10){
						i = "0"+i;
					}
					html7 += '<input class="type_7 type_span" title="'+i+'" disabled type="checkbox" />';

				}
				$("#type_box_7").html(html7);

				var html8 = '';
				for (var i = 1; i < type_box_8; i++) {
					if(i < 10){
						i = "0"+i;
					}
					html8 += '<input class="type_8 type_span" title="'+i+'" disabled type="checkbox" />';

				}
				$("#type_box_8").html(html8);

				var html9 = '';
				for (var i = 1; i < type_box_9; i++) {
					if(i < 10){
						i = "0"+i;
					}
					html9 += '<input class="type_9 type_span" title="'+i+'" disabled type="checkbox" />';

				}
				$("#type_box_9").html(html9);
				form.render('checkbox');
			}else{
				$("#topic_list").hide();
				$("#topic_list span").removeClass("selected");
				$("#topic_list_box").attr('src', '');
				$("#topic_list_box").hide();
			}
			$("input[type=hidden].selected_topic").remove();
		}

		$("#chapter_id").click(function(){
			/*$(".acc-data-selector").show();
			$(".acc-data-selector-table").show();*/
			layer.open({
		  		type: 1 //Page层类型
			  	,area: ['573px', '500px']
			  	,title: '请选择章节'
			  	,shade: 0.6 //遮罩透明度
			  	,shadeClose :true
			  	,maxmin: true //允许全屏最小化
			  	,anim: 1 //0-6的动画形式，-1不开启
			  	,content: $(".acc-data-selector")
			});    
		})
	});

	$(".ico-check").click(function(){
		var parent = $(this).parent();
		var topic_id = parent.attr("topic_id");
		if(parent.hasClass("checked")){
			parent.removeClass("checked");
			$(".chapter_"+topic_id).remove();
			$("input[name='chapter_id["+topic_id+"]']").remove();
		}else{
			parent.addClass("checked");

			var chapter_name = parent.text();
			var ul = parent.parent().find('li:first');
			var subject_name = ul.text().substr(0, ul.text().indexOf(" "));
			var select_chapter_name = subject_name + ' ' + chapter_name;
			var element_html = '<span class="item chapter_'+topic_id+'" topic_id="'+topic_id+'" style="display: inline-block;"><span title="'+select_chapter_name+'">'+select_chapter_name+'</span><i class="iclose act-checkItems">×</i></span>';
			element_html += '<input type="hidden" class="chapter_id" name="chapter_id['+topic_id+']" value="'+topic_id+'">';
			parent.closest(".acc-data-selector").find(".acc-data-selector-selected").append(element_html)

			var element_html = '<span style="padding-right:20px;" class="item chapter_'+topic_id+'" style="display: inline-block;"><span title="'+select_chapter_name+'">'+select_chapter_name+'</span></span>';
			$("#selected_chapter").append(element_html)
		}
	})

	$(".act-showItems").click(function(){
		var _this = $(this);
		var index = _this.index();
		if(_this.hasClass("active") == false){
			$(".act-showItems").removeClass("active")
			_this.addClass("active")
			$(".acc-data-selector-list").hide();
			$(".acc-data-selector-list").eq(index).show();
		}
	})

	$(document).on("click", '.iclose', function(){
		var topic_id = $(this).parent().attr("topic_id");
		$(".chapter_"+topic_id).remove();
		$("input[name='chapter_id["+topic_id+"]']").remove();
		$("li[topic_id="+topic_id+"]").removeClass("checked");
	})

	$("form").submit(function(e){
		if($("#template_id").val() <= 0){
			var msg = '';
			if($("#template_id").val() == ''){
				msg = '请选择组卷模版';
			}else{
				msg = '请先保存组卷模版';
			}
			layer.alert(msg, {
		  		skin: 'layui-layer-molv' //样式类名
			  	,closeBtn: 0
			  	,anim: 4 //动画类型
			});
			return false;
		}
		if($(".selected_topic").length <= 0 && $("input[name='create_topic_type']:checked").val() == 2){
			layer.alert("请选择题目", {
		  		skin: 'layui-layer-molv' //样式类名
			  	,closeBtn: 0
			  	,anim: 4 //动画类型
			});
			return false;
		}
	});
})